package com.exchange.controller.app.work;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.aliyun.oss.OSSClient;
import com.exchange.common.model.User;
import com.exchange.common.model.WorkOrder;
import com.exchange.common.model.WorkOrderDetails;
import com.exchange.util.aliOSSFileUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

public class WorkOrderService {
	
	int pageSize = 20;
	
	public Ret query(User user, Integer pageNumber){
		Page<WorkOrder> workOrderPage = WorkOrder.dao.paginate(pageNumber, pageSize, "SELECT * ", "FROM t_work_order WHERE userId = ?", user.getId());
		return Ret.ok("workOrderPage", workOrderPage);
    }
	
	public Ret modify(User user, List<UploadFile> materials, String question, String reason){
		
		String imgstr = "";
		for(UploadFile uf : materials) {
			OSSClient ossClient = aliOSSFileUtil.getOSSClient();
	        String picUrl = aliOSSFileUtil.uploadImg(ossClient, uf.getFile(), PropKit.get("bucketApp"));
	        imgstr = imgstr+","+ picUrl;
        }
		Date date = new Date();
		new WorkOrder().setQuestion(question).setReason(reason).setMaterial(imgstr).setUserId(user.getId())
		.setState(1).setCreateTime(date).setVersion(1).setUpdateTime(date)
		.save();
		
		return Ret.ok();
    }
	
	public Ret reply(User user, Integer workId, String content){
		
		new WorkOrderDetails().setWorkId(workId).setContent(content).setUserId(user.getId()).setCreateTime(new Date()).save();
		
		return Ret.ok();
    }
	
	
}
